package com.qqhf.hitfood.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qqhf.common.entity.AjaxResult;
import com.qqhf.common.entity.PageEntity;
import com.qqhf.hitfood.bean.core.BaseController;
import com.qqhf.hitfood.bean.entity.AcctInfo;
import com.qqhf.hitfood.bean.entity.AcctUser;
import com.qqhf.hitfood.service.IAcctUserService;
import com.qqhf.security.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

// User 消费用户用这个
@Api(tags = "A - 用户")
@RestController
@RequestMapping("/acct/user")
public class AcctUserController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(AcctUserController.class);
    @Autowired
    private IAcctUserService acctUserService;

    @Operation(summary = "id获取用户信息")
    @GetMapping(value = "/info")
    public AjaxResult getUser(@RequestParam("userId") Integer userId) {
        return AjaxResult.success(acctUserService.getById(userId));
    }

    @Operation(summary = "注册用户")
    @PostMapping(value = "/register")
    public AjaxResult register(
            @RequestParam("name") String name,
            @RequestParam("pwd") String pwd,
            @RequestParam("phone") String phone,
            @RequestParam("number") String number) {
        AcctUser acctUser = new AcctUser();
        AcctUser name1 = acctUserService.getOne(new QueryWrapper<AcctUser>().eq("name", name), false);
        if (name1 != null) {
            return AjaxResult.error("该用户已存在");
        }
        acctUser.setName(name);
        acctUser.setPassword(pwd);
        acctUser.setPhone(phone);
        acctUser.setNumber(number);
        acctUserService.save(acctUser);
        return AjaxResult.success("注册成功");
    }

    @Operation(summary = "用户登录")
    @GetMapping(value = "/login")
    public Object login(
            @RequestParam("username") String userName,
            @RequestParam("password") String password) {
        AcctUser acctUser = acctUserService.getOne(new QueryWrapper<AcctUser>().eq("name", userName), false);
        if (acctUser == null) {
            return AjaxResult.error("没有该账号");
        }
        //String passwdMd5 = MD5.md5(password, acctWaiter.getSalt());
        if (!acctUser.getPassword().equals(password)) {
            return AjaxResult.error("输入的密码错误");
        }
        if (acctUser.getStId() == 2) {
            return AjaxResult.error("该账号已冻结");
        }
        if (acctUser.getStId() == 3) {
            return AjaxResult.error("该账号已停用");
        }
        setSession("currentUser", acctUser);
        AcctInfo acctInfo = new AcctInfo();
        BeanUtils.copyProperties(acctUser, acctInfo);
        String token = JwtUtil.sign(acctInfo);
        Map<String, String> result = new HashMap<>(1);
        result.put("token", token);
        logger.info("token:{}", token);
        return AjaxResult.success(acctInfo);
    }

    @Operation(summary = "修改用户名")
    @PutMapping(value = "/chgName")
    public Object chgName(
            @RequestParam("userId") Integer userId,
            @RequestParam("newName") String newName) {
        AcctUser acctUser = acctUserService.getById(userId);
        acctUser.setName(newName);
        acctUserService.updateById(acctUser);
        return AjaxResult.success(acctUser);
    }

    @Operation(summary = "修改学号/工号")
    @PutMapping(value = "/chgNum")
    public Object chgNum(
            @RequestParam("userId") Integer userId,
            @RequestParam("newNum") String newNum) {
        AcctUser acctUser = acctUserService.getById(userId);
        acctUser.setNumber(newNum);
        acctUserService.updateById(acctUser);
        return AjaxResult.success(acctUser);
    }

    @Operation(summary = "修改密码")
    @PutMapping(value = "/chgPwd")
    public Object chgPwd(
            @RequestParam("userId") Integer userId,
            @RequestParam("oldPwd") String oldPwd,
            @RequestParam("newPwd") String newPwd) {
        AcctUser acctUser = acctUserService.getById(userId);
        if (acctUser != null && acctUser.getPassword().equals(oldPwd)) {
            acctUser.setPassword(newPwd);
            if (acctUserService.updateById(acctUser))
                return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("账号或密码错误");
    }

    @Operation(summary = "重置密码")
    @PutMapping(value = "/initPwd")
    public Object initPwd(
            @RequestParam("name") String name,
            @RequestParam("number") String number) {
        AcctUser acctUser = acctUserService.getOne(new QueryWrapper<AcctUser>().eq("name", name).eq("number", number));
        if (acctUser != null) {
            acctUser.setPassword("666666");
            if (acctUserService.updateById(acctUser))
                return AjaxResult.success("重置成功");
        }
        return AjaxResult.error("用户名或号码错误");
    }

    @GetMapping("/page")
    public AjaxResult page(PageEntity pageEntity, AcctUser acctUser) {
        return AjaxResult.success(acctUserService.page(pageEntity.toPage(), Wrappers.lambdaQuery(acctUser)));
    }

    @Operation(summary = "获取用户列表")
    @GetMapping("/list")
    public AjaxResult list(AcctUser acctUser) {
        return AjaxResult.success(acctUserService.list(Wrappers.lambdaQuery(acctUser)));
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody AcctUser acctUser) {
        return AjaxResult.success(acctUserService.save(acctUser));
    }

    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody AcctUser acctUser) {
        return AjaxResult.success(acctUserService.updateById(acctUser));
    }

    @DeleteMapping("/delete/{ids}")
    public AjaxResult delete(@PathVariable Integer[] ids) {
        return AjaxResult.success(acctUserService.removeByIds(Arrays.asList(ids)));
    }

    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable("id") Integer id) {
        return AjaxResult.success(acctUserService.getById(id));
    }


}
